package com.zzw.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzw.springboot.common.Result;
import com.zzw.springboot.entity.Comment;
import com.zzw.springboot.service.ICommentService;
import com.zzw.springboot.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author zzw2000
 * @Date 2022-06-12 16:42:47
 * @Description Comment前端控制器
 */
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ICommentService commentService;

    /**
     * 保存
     *
     * @param comment
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Comment comment) {
        //新增评论
        if (Objects.isNull(comment.getId())) {
            comment.setUserId(JwtUtils.getCurrentUser().getId());
            comment.setTime(DateUtil.now());

            Integer pid = comment.getPid();
            // 判断如果是回复，进行处理
            if (Objects.nonNull(pid)) {
                Comment pComment = commentService.getById(pid);
                // 如果当前回复的父级有祖宗，那么就设置相同的祖宗
                if (Objects.nonNull(pComment.getOriginId())){
                    comment.setOriginId(pComment.getOriginId());
                }else { // 否则就设置父级为当前回复的祖宗
                    comment.setOriginId(pid);
                }
            }
        }
        return Result.success(commentService.saveOrUpdate(comment));
    }

    /**
     * 根据id删除数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(commentService.removeById(id));
    }

    /**
     * 根据id批量删除comment
     *
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result delete(@RequestBody List<Integer> ids) {
        return Result.success(commentService.removeBatchByIds(ids));
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    public Result findAll() {
        return Result.success(commentService.list());
    }

    /**
     * 查询单条记录
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(commentService.getById(id));
    }

    /**
     * 分页查询
     *
     * @param pageNum  当前页码
     * @param pageSize 每页显示的条数
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        IPage<Comment> commentPage = commentService.page(new Page<>(pageNum, pageSize));
        return Result.success(commentPage);
    }

    @GetMapping("/tree/{id}")
    public Result findTree(@PathVariable Integer id) {
        return Result.success(commentService.findCommentDetail(id));
    }
}

